package lar.game.gamestate;

import javax.microedition.khronos.opengles.GL11;

import android.util.Log;
import android.view.MotionEvent;

import lar.system.engine.MainProgram;
import lar.system.engine.TouchListener;
import lar.system.opengl.GLButton;
import lar.system.opengl.GLEmptyQuad;
import lar.system.R;


public class PauseMenu implements GameState 
{
	private GameState returnState;
	private GLButton unPauseButton;
	
	public PauseMenu(GameState prevState)
	{
		///Log.d("R", "entered pause menu");
		returnState = prevState;
		unPauseButton = new GLButton(R.drawable.resume, new float[]{0f,0.8f}, new float[]{0.5f, 0.25f}, 
				new TouchListener() 
				{
					public void onTouchEvent(MotionEvent e) 
					{
						//Log.d("R", "exited pause menu " + MainProgram.gameLoop.getState().getClass().getName());
						MainProgram.glSurfaceView.PopClearListeners();
						MainProgram.gameLoop.ChangeState(returnState);
					}
				});
		float asp = MainProgram.glSurfaceView.getAspectRatio();
		
	}
	public void gameFrame() 
	{
		
			MainProgram.glSurfaceView.PopClearListeners();
			MainProgram.gameLoop.ChangeState(returnState);
		
	}

	public void Draw(GL11 gl) 
	{
		returnState.Draw(gl);
		
		gl.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
		
		gl.glFrontFace(GL11.GL_CW);
		
		gl.glVertexPointer(3, GL11.GL_FLOAT, 0, GLEmptyQuad.vertexBuffer);
		gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, GLEmptyQuad.textureBuffer);
		
		float asp = MainProgram.glSurfaceView.getAspectRatio();
		gl.glPushMatrix();
		gl.glColor4f(0f, 0f, 0f, 0.4f);
		gl.glTranslatef(0.0f, 0.0f, -1.0f);
		gl.glScalef(asp, 1.0f, 1.0f);
		gl.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, GLEmptyQuad.vertices.length / 3);
		gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
		gl.glPopMatrix();
		gl.glDisableClientState(GL11.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
		//Log.d("R", "drawing...");
		unPauseButton.Draw(gl);
		
	}

	public GameState getReturnState()
	{
		return returnState;
	}
	@Override
	public void onPause() 
	{
		returnState.onPause();
	}
	@Override
	public void onResume() 
	{
		returnState.onResume();
		
			MainProgram.glSurfaceView.PopClearListeners();
			MainProgram.gameLoop.ChangeState(returnState);
		
	}
	@Override
	public boolean CheckBounds(float x, float y) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onBack() 
	{
		MainProgram.glSurfaceView.PopClearListeners();
		MainProgram.gameLoop.ChangeState(returnState);	
	}
	
}
